<?php


namespace App\Http\Controllers\Admin;


use App\Jobs\ProcessPodcast;
use App\Tool\Json;
use Illuminate\Redis\Connections\PredisConnection;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Redis;
use function GuzzleHttp\Promise\queue;

class QueueController extends AuthCheckController
{

    protected $list;


    function __construct()
    {
        parent::__construct();

//        $this->list=env()

        $list=env('QUEUE_LIST');

        $list=explode(',',$list);

        $this->list=$list;

    }

    /**
     * @Auth(type='no_check')
     * Create by Peter
     * 2019/09/06 16:28:28
     * Email:904801074@qq.com
     * @return false|string
     */
    function send()
    {

//        $this->dispatch()

//        dispatch();


//        for ($i = 0; $i < 100; $i++) {
            dispatch(new ProcessPodcast('904801074@qq.com', '测试1','<h1>测试内容</h1>'))

//               ->delay(now()->addSeconds($i+1))
                ->onQueue('high')->delay(now()->addSeconds(50));
//        }

//       $re=Queue::size('high');

//       dd($re);


//       dispatch(new ProcessPodcast('904801074@qq.com','low'))->onQueue('low');
//       dispatch(new ProcessPodcast('904801074@qq.com','low'))->onQueue('low');
//       dispatch(new ProcessPodcast('904801074@qq.com','low'))->onQueue('low');


//        \Mail::send('mail.mail', [], function (\Illuminate\Mail\Message $message) {
//
//            //发送给两个人
//            $message->to('904801074@qq.com');
//            $message->to('15827104268@163.com');
//
//            $message->subject('content');
//
//
//        });

//        return Json::code(1,'success');
    }


    /**
     * 获取管道列表
     * Create by Peter
     * 2020/05/19 10:38:30
     * Email:904801074@qq.com
     * @return false|string
     */
    function getPip(){





        return Json::code(1,'success',$this->list);
    }


    /**
     * 获取即时队列数据
     * @Auth(type='no_check')
     * Create by Peter
     * 2019/09/06 16:28:28
     * Email:904801074@qq.com
     * @return false|string
     */
    function getTask()
    {


        $p = request()->input('p', 1);

        $pip=request()->input('pip','default');

        $num = 10;

        $offset = ($p - 1) * $num;

        $l = Redis::lrange('queues:'.$pip, $offset, $num + $offset - 1);

        $temp = [];

        foreach ($l as $key => $value) {

            $task = json_decode($value, true);

            $command = (array)unserialize($task['data']['command']);

            $task['params'] = $command;

            $temp[] = $task;

        }

        return Json::code(1, 'success', ['data' => $temp, 'total' => Redis::llen('queues:'.$pip)]);
    }


    /**
     * 获取延迟数据
     * @Auth(type='no_check')
     * Create by Peter
     * 2019/09/11 14:33:40
     * Email:904801074@qq.com
     */
    function getDelayTask()
    {

        $pip=request()->input('pip','default');

        $p = request()->input('p', 1);

        $num = 10;

        $offset = ($p - 1) * $num;

        $l = Redis::zrange('queues:'.$pip.':delayed', $offset, $num + $offset - 1,'withscores');


        $temp = [];

        foreach ($l as $value => $key) {

            $task = json_decode($value, true);

            $command = (array)unserialize($task['data']['command']);

            $task['params'] = $command;

            $task['runTime']=date('Y-m-d H:i:s',$key);

            $temp[] = $task;

        }

        return Json::code(1, 'success', ['data' => $temp, 'total' => Redis::zcard('queues:'.$pip.':delayed')]);

    }


}
